package com.ciwong.epaper.util;

import android.util.Base64;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.io.UnsupportedEncodingException;

/**
 * Created by Administrator on 2017/5/5.
 */
public class Base64Utils {

    // 加密
    public static String getBase64(String str) {
        String result = "";
        if( str != null) {
            try {
                result = new String(Base64.encode(str.getBytes("utf-8"), Base64.NO_WRAP),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    // 解密
    public static String getFromBase64(String str) {
        String result = "";
        if (str != null) {
            try {
                result = new String(Base64.decode(str, Base64.NO_WRAP), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param baseActivity
     * @param versionId
     * @param parentVersionId
     * @param doWorkId
     * @return
     */
    public static String getShareBase64Url(BaseActivity baseActivity,String hostUrl ,String versionId,String parentVersionId,String doWorkId){
        String shareUrl;
        StringBuffer sb_hostUrl=new StringBuffer(hostUrl);
        StringBuffer sb=new StringBuffer();
        sb.append("pageType=2");
        sb.append("&clientId=");
        sb.append(EConstants.CLIENT_ID);
        long userId=baseActivity.getUserInfoBase().getUserId();
        sb.append("&userId=");
        sb.append(userId);
        sb.append("&brandId=");
        sb.append(EApplication.BRAND_ID);
        sb.append("&versionId=");
        sb.append(versionId);
        sb.append("&parentVersionId=");
        sb.append(parentVersionId);
        sb.append("&doWorkId=");
        sb.append(doWorkId);
        String param= Base64Utils.getBase64(sb.toString());
        shareUrl=sb_hostUrl.append(param).toString();
        return shareUrl;
    }
}
